package com.lagou.controller;

import com.lagou.domain.Course;
import com.lagou.domain.CourseVO;
import com.lagou.domain.ResponseResult;
import com.lagou.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 多条件查询课程列表
     *
     * @param courseVO
     * @return
     */
    @RequestMapping("/findCourseByCondition")
    public ResponseResult findCourseByCondition(@RequestBody CourseVO courseVO) {

        List<Course> list = courseService.findCourseByCondition(courseVO);
        return new ResponseResult(true, 200, "查询成功", list);
    }

    /**
     * 图片上传接口
     *
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/courseUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        if (file.isEmpty()) {

            throw new RuntimeException();
        }

        String realPath = request.getServletContext().getRealPath("/");
        String webappsPath = realPath.substring(0, realPath.indexOf("ssm-web"));
        String fileName = file.getOriginalFilename();
        String newFileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
        String uploadPath = webappsPath + "upload\\";
        File file1 = new File(uploadPath, newFileName);
        if (!file1.getParentFile().exists()) {

            file1.getParentFile().mkdirs();
            System.out.println("创建目录：" + file1);
        }

        try {
            file.transferTo(file1);
            Map<String, String> map = new HashMap<>();
            map.put("fileName", newFileName);
            map.put("filePath", "http:localhost:8080/upload/" + newFileName);
            return new ResponseResult(true, 200, "图片上传成功", map);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 保存&修改课程信息接口
     *
     * @param courseVO
     * @return
     */
    @RequestMapping("/saveOrUpdateCourse")
    public ResponseResult saveOrUpdateCourse(@RequestBody CourseVO courseVO) {

        try {

            String msg;
            if (courseVO.getId() == null) {

                courseService.saveCourseOrTeacher(courseVO);
                msg = "保存课程信息成功";
            } else {

                courseService.updateCourseOrTeacher(courseVO);
                msg = "修改课程信息成功";
            }

            return new ResponseResult(true, 200, msg, null);
        } catch (InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 根据id获取课程信息
     *
     * @param id 课程id
     * @return
     */
    @RequestMapping("/findCourseById")
    public ResponseResult findCourseById(Integer id) {

        CourseVO courseVO = courseService.findCourseById(id);
        return new ResponseResult(true, 200, "获取课程信息成功", courseVO);
    }

    /**
     * 修改课程状态
     *
     * @param id     课程id
     * @param status 课程状态
     * @return
     */
    @RequestMapping("/updateCourseStatus")
    public ResponseResult updateCourseStatus(Integer id, Integer status) {

        courseService.updateCourseStatus(id, status);
        Map<String, Integer> map = new HashMap<>();
        map.put("status", status);
        return new ResponseResult(true, 200, "修改课程状态成功", map);
    }


}
